<?php
ob_start();
session_start();
/**
 * Dragons Fun copy
 * @author aanbek
 */
$start_time = microtime(true);
$config = __DIR__ . '/data/config.php';
if (!file_exists($config)) {
    throw new Exception("Game not installed. Read install instructions from Readme.md");
}
$config = require_once $config;
// others
require_once __DIR__ . '/filedb.php';
$fDb = new filedb();
$fDb->setPath(__DIR__ . '/filedb');
function post($key, $default = false) {
    if (isset($_POST[$key])) {
        return $_POST[$key];
    }
    return $default;
}
function loadModel($name, $db)
{
    $pathModels = __DIR__ . '/';
    $mPath = sprintf("%s/%s.php", $pathModels, $name);
    if (!file_exists($mPath)) {
        throw new Exception("Model not found");
    }
    require $mPath;
    if (!class_exists($cName = sprintf("model%s", ucfirst($name)))) {
        throw new Exception("Model class not found");
    }
    return new $cName($db);
}
function __autoload($file)
{
    global $config;
    $path = $config['root'] . '/inc/' . $file . '.php';
    if (file_exists($path)) {
        include_once $path;
    } else {
        die ('Class ' . $file . ' not found! ');
    }
}
try {
    $database1 = new PDO('mysql:host=' . $config['dbhost'] . ';dbname=' . $config['dbname'] . '',
        $config['dbuser'], $config['dbpass']);
} catch (PDOException $e) {
    throw $e;
}
$db = new db($database1);
$db->noPrepared("SET sql_mode = ''");
if (isset($_SESSION['id']) && isset($_SESSION['password'])) {
    $sqlSession = "SELECT * FROM `users` WHERE `id`=? and `password`=? LIMIT 1";
    $plaseholdersSession = array($_SESSION['id'], $_SESSION['password']);
    $rowsSession = $db->rows($sqlSession, $plaseholdersSession);
    if ($rowsSession == 0) {
        $_SESSION['id'] = null;
        $_SESSION['password'] = null;
        header("Location:/");
        exit;
    }
    $user = $db->fetch($sqlSession, $plaseholdersSession);
}
if (isset($user)) {
    if (isset($_GET['logout'])) {
        $_SESSION['id'] = null;
        $_SESSION['password'] = null;
        header("Location:/");
        exit;
    }
    $sqlOnline = "UPDATE `users` SET `online`=? WHERE `id`=?";
    $plaseholdersOnline = array($config['time'], $user['id']);
    $updateUsersOnline = $db->query($sqlOnline, $plaseholdersOnline);
    $expLevel = require __DIR__ . '/exp.php';
}
////including header main
if($_SERVER['PHP_SELF'] != '/reg.php'){
include_once __DIR__ . '/head.php';
}
//////////////////
if (isset($paginate) && $paginate === 1) {
    include_once 'page.php';
}
